/*=============================================================================
*
*   Copyright (C) 2020 All rights reserved.
*
*   Filename:		regex.hpp
*
*   Author: Wang Zhecheng - wangzhecheng@yeah.net
*
*   Date: 2020-03-18 15:45
*
*   Last Editors: Wang Zhecheng - wangzhecheng@yeah.net
*
*   Last modified:	2020-03-18 16:22
*
*   Description:
*
=============================================================================*/
#ifndef REGEX_H
#define REGEX_H

#include <regex>
#include <string>

namespace geoxfem {
namespace util {

struct ignore_case {
  static std::string apply(const std::string& str) {
    std::string str_lower = str;
    std::transform(str_lower.begin(), str_lower.end(), str_lower.begin(),
                   (int (*)(int))std::tolower);
    return str_lower;
  }
  static constexpr std::regex_constants::syntax_option_type flag_type =
    std::regex_constants::icase;
};
struct case_sensitive {
  static std::string apply(const std::string& str) {
    return str;
  }
  static constexpr std::regex_constants::syntax_option_type flag_type =
    std::regex_constants::ECMAScript;
};

template <typename CasePolicy = ignore_case>
std::pair<bool, std::string> parseName(const std::string& src,
                                       const std::string& patter_str,
                                       const std::size_t& index = 0) {
  std::smatch result;
  std::regex  pattern(patter_str, CasePolicy::flag_type);

  if(std::regex_match(src, result, pattern))
    return {true, std::string(result[index])};
  else
    return {false, std::string()};
};

}  // namespace util
}  // namespace geoxfem

#endif /* REGEX_H */
